####### SCRIPT FOR: 
####### The Role of Ethnic Identity in the Perception of Pork-Barrel Politics: 
####### Evidence from a Survey Experiment in Slovakia

rm(list=ls())
gc()

setwd("C:/Users/miros/Desktop/Research/Ethnic link in perception of pork-barrel")

#Data
library(haven)
data <- read_sav("exp.sav")
View(data)
summary(data)
table(data$NAT)

#Adding dummies
#Respondent benefits
data$DM_Ben <- NA
data$DM_Ben[data$SKUPINA == 1] <- "Benefit for Slovakia"
data$DM_Ben[data$SKUPINA == 2] <- "Benefit for Slovakia"
data$DM_Ben[data$SKUPINA == 3] <- "Benefit for Hungary"
data$DM_Ben[data$SKUPINA == 4] <- "Benefit for Hungary"
data$DM_Ben <- as.factor(data$DM_Ben)
is.factor(data$DM_Ben)
table(data$DM_Ben)

#Distribution fairness
data$Fairness <- NA
data$Fairness[data$SKUPINA == 1] <- "Fair distribution"
data$Fairness[data$SKUPINA == 2] <- "Unfair distribution"
data$Fairness[data$SKUPINA == 3] <- "Fair distribution"
data$Fairness[data$SKUPINA == 4] <- "Unfair distribution"
data$Fairness <- as.factor(data$Fairness)
is.factor(data$Fairness)
table(data$Fairness)

#Recoding nationality
data$Nationality <- NA
data$Nationality[data$NAT == 1] <- "Slovak"
data$Nationality[data$NAT == 2]     <- "Hungarian"
data$Nationality[data$NAT == 3] <- NA
data$Nationality <- as.factor(data$Nationality)
is.factor(data$Nationality)
table(data$Nationality)

#Coding shared ethnicity
data$SharedEtn <- NA
data$SharedEtn[data$NAT == 1 & data$SKUPINA == 1] <- "Shared ethnicity"
data$SharedEtn[data$NAT == 1 & data$SKUPINA == 2] <- "Shared ethnicity"
data$SharedEtn[data$NAT == 1 & data$SKUPINA == 3] <- "Different ethnicity"
data$SharedEtn[data$NAT == 1 & data$SKUPINA == 4] <- "Different ethnicity"
data$SharedEtn[data$NAT == 2 & data$SKUPINA == 1] <- "Different ethnicity"
data$SharedEtn[data$NAT == 2 & data$SKUPINA == 2] <- "Different ethnicity"
data$SharedEtn[data$NAT == 2 & data$SKUPINA == 3] <- "Shared ethnicity"
data$SharedEtn[data$NAT == 2 & data$SKUPINA == 4] <- "Shared ethnicity"
data$SharedEtn <- as.factor(data$SharedEtn)
is.factor(data$SharedEtn)
table(data$SharedEtn)

#Renaming variables for questions
data$Q1TRUST <- data$Q1
data$Q1 <- NULL
data$Q2VOTE <- data$Q2
data$Q2 <- NULL
data$Q3SUPPORT <- data$Q3
data$Q3 <- NULL


#Model 1: Trust in Politican
#model: shared ethnicities
m1.trust <- lm(Q1TRUST ~ SharedEtn, data = data)
summary(m1.trust)

m2.trust <- lm(Q1TRUST ~ SharedEtn + Nationality + Fairness, data = data)
summary(m2.trust)

m3.trust <- lm(Q1TRUST ~ SharedEtn + Nationality + DM_Ben, data = data)
summary(m3.trust)

m4.trust <- lm(Q1TRUST ~ SharedEtn + Nationality + DM_Ben + Fairness, data = data)
summary(m4.trust)

#Trust: Shared ethnicity models output
library(stargazer)
stargazer(m1.trust, m2.trust, m3.trust, m4.trust,
          type="html", out="models1-4_shared_trust.htm")


#model: different ethnicities
m5.trust <- lm(Q1TRUST ~ DM_Ben + Nationality + Fairness, data = data)
summary(m5.trust)

m6.trust <- lm(Q1TRUST ~ DM_Ben*Nationality + Fairness*Nationality, data = data)
summary(m6.trust)

m7.trust <- lm(Q1TRUST ~ DM_Ben*Nationality*Fairness, data = data)
summary(m7.trust)

#Trust: Models output
library(stargazer)
stargazer(m5.trust, m6.trust, m7.trust,
          type="html", out="models5-7_SKvsHU_trust.htm")


#Figure 1: Trust in Politican
library(ggplot2)
library(sjPlot)
library(sjmisc)
#png(file = "Fig1_trust.png", width = 4000, height = 2400, res = 600)
f1.trust <- plot_model(m5.trust, type = "int",
                       #terms = c("Fairness", "SharedEtn"),
                       #line.size = 1,
                       #dot.size = 2,
                       colors = "gs",
                       legend.title = "Ethnicity:") +
  coord_cartesian(xlim = c(0.75, 2.25), ylim = c(0, 10), expand = FALSE) +
  labs(x = "",
       y = "Trust in decision-maker") +
  scale_x_reverse(breaks = c(1, 2),
                  labels = c("Hungarian\ndecision-maker\n(Benefit for HUN)", 
                             "Slovak\ndecision-maker\n(Benefit for SVK)")) +
  geom_hline(yintercept = 0) +
  geom_hline(yintercept = 10) +
  theme(plot.title = element_blank(),
        axis.title.x = element_blank(),
        #text = element_text(size = 30),
        legend.position="bottom",
        legend.background = element_rect(colour = 'black', fill = 'white', linetype='solid'),
        legend.key=element_blank(),
        panel.border = element_rect(fill = "transparent"),
        strip.background = element_rect(colour = "black"),
        panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank(),
        panel.grid.major.y = element_line(colour = "gray90"),
        panel.grid.minor.y = element_line(colour = "gray90"),
        panel.background = element_blank())
f1.trust
#dev.off()

#Models: Vote for Politican
#model: shared ethnicities
m1.vote <- lm(Q2VOTE ~ SharedEtn, data = data)
summary(m1.vote)

m2.vote <- lm(Q2VOTE ~ SharedEtn + Nationality + Fairness, data = data)
summary(m2.vote)

m3.vote <- lm(Q2VOTE ~ SharedEtn + Nationality + DM_Ben, data = data)
summary(m3.vote)

m4.vote <- lm(Q2VOTE ~ SharedEtn + Nationality + DM_Ben + Fairness, data = data)
summary(m4.vote)

#Trust: Shared ethnicity models output
library(stargazer)
stargazer(m1.vote, m2.vote, m3.vote, m4.vote,
          type="html", out="models1-4_shared_vote.htm")


#model: different ethnicities
m5.vote <- lm(Q2VOTE ~ DM_Ben + Nationality + Fairness, data = data)
summary(m5.vote)

m6.vote <- lm(Q2VOTE ~ DM_Ben*Nationality + Fairness*Nationality, data = data)
summary(m6.vote)

m7.vote <- lm(Q2VOTE ~ DM_Ben*Nationality*Fairness, data = data)
summary(m7.vote)


#Vote: Models output
library(stargazer)
stargazer(m5.vote, m6.vote, m7.vote, 
          type="html", out="models5-7_SKvsHU_vote.htm")

#Figure 2: Vote for Politican
library(ggplot2)
library(sjPlot)
library(sjmisc)
#png(file = "Fig2_vote.png", width = 4000, height = 2400, res = 600)
f2.vote <- plot_model(m5.vote, type = "int",
                      #line.size = 1,
                      #dot.size = 2,
                      colors = "gs",
                      legend.title = "Ethnicity:") +
  coord_cartesian(xlim = c(0.75, 2.25), ylim = c(0, 10), expand = FALSE) +
  scale_x_reverse(breaks = c(1, 2),
                  labels = c("Hungarian\ndecision-maker\n(Benefit for HUN)", 
                             "Slovak\ndecision-maker\n(Benefit for SVK)")) +
  labs(x = "",
       y = "Willingness to vote for decision-maker") +
  geom_hline(yintercept = 0) +
  geom_hline(yintercept = 10) +
  theme(plot.title = element_blank(),
        axis.title.x = element_blank(),
        #text = element_text(size = 30),
        legend.position="bottom",
        legend.background = element_rect(colour = 'black', fill = 'white', linetype='solid'),
        legend.key=element_blank(),
        panel.border = element_rect(fill = "transparent"),
        strip.background = element_rect(colour = "black"),
        panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank(),
        panel.grid.major.y = element_line(colour = "gray90"),
        panel.grid.minor.y = element_line(colour = "gray90"),
        panel.background = element_blank())
f2.vote
#dev.off()

#Model 3: Support for policy
#model: shared ethnicities
m1.support <- lm(Q3SUPPORT ~ SharedEtn, data = data)
summary(m1.support)

m2.support <- lm(Q3SUPPORT ~ SharedEtn + Nationality + Fairness, data = data)
summary(m2.support)

m3.support <- lm(Q3SUPPORT ~ SharedEtn + Nationality + DM_Ben, data = data)
summary(m3.support)

m4.support <- lm(Q3SUPPORT ~ SharedEtn + Nationality + DM_Ben + Fairness, data = data)
summary(m4.support)

#Trust: Shared ethnicity models output
library(stargazer)
stargazer(m1.support, m2.support, m3.support, m4.support,
          type="html", out="models1-4_shared_support.htm")

#model
m5.support <- lm(Q3SUPPORT ~ DM_Ben + Nationality + Fairness, data = data)
summary(m5.support)

m6.support <- lm(Q3SUPPORT ~ DM_Ben*Nationality + Fairness*Nationality, data = data)
summary(m6.support)

m7.support <- lm(Q3SUPPORT ~ DM_Ben*Nationality*Fairness, data = data)
summary(m7.support)

#Support for policy: Models output
library(stargazer)
stargazer(m5.support, m6.support, m7.support, 
          type="html", out="models5-7_SKvsHU_support.htm")


#Figure 3: Support for policy
library(ggplot2)
library(sjPlot)
library(sjmisc)
#png(file = "Fig3_support.png", width = 4000, height = 2400, res = 600)
f3.support <- plot_model(m3.support, type = "int",
                         #line.size = 1,
                         #dot.size = 2,
                         colors = "gs",
                         legend.title = "Ethnicity:") +
  coord_cartesian(xlim = c(0.75, 2.25), ylim = c(0, 10), expand = FALSE) +
  scale_x_reverse(breaks = c(1, 2),
                  labels = c("Hungarian\ndecision-maker\n(Benefit for HUN)", 
                             "Slovak\ndecision-maker\n(Benefit for SVK)")) +
  labs(x = "",
       y = "Support for policy") +
  geom_hline(yintercept = 0) +
  geom_hline(yintercept = 10) +
  theme(plot.title = element_blank(),
        axis.title.x = element_blank(),
        #text = element_text(size = 30),
        legend.position="bottom",
        legend.background = element_rect(colour = 'black', fill = 'white', linetype='solid'),
        legend.key=element_blank(),
        panel.border = element_rect(fill = "transparent"),
        strip.background = element_rect(colour = "black"),
        panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank(),
        panel.grid.major.y = element_line(colour = "gray90"),
        panel.grid.minor.y = element_line(colour = "gray90"),
        panel.background = element_blank())
f3.support
#dev.off()
